//
//  PopSortingView.m
//  NoneCar
//
//  Created by DongSen on 2018/7/17.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "SortTableViewCell.h"
#import "PopSortingView.h"

@interface PopSortingView()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation PopSortingView
+ (PopSortingView*)loadView{
    NSArray * viewArray =[[NSBundle mainBundle] loadNibNamed:@"PopSortingView" owner:self options:nil];
    PopSortingView * view  = [viewArray firstObject];
    [view.dataArray addObjectsFromArray:@[@"流入时间最近",@"流入时间最远",@"报案时间最近",@"报案时间最远"]];
    view.tableView.delegate = view;
    view.tableView.dataSource = view;
    view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [view.tableView registerNib:[UINib nibWithNibName:@"SortTableViewCell" bundle:nil] forCellReuseIdentifier:@"SortTableViewCell"];
    view.contentHeight.constant = 0;
    return view;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SortTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SortTableViewCell"];
    cell.titlesLabel.text = self.dataArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.block) {
        self.block(self.dataArray[indexPath.row]);
    }
    [self removieView];
}
-(void)showView{
    self.show=YES;
    [self layoutIfNeeded];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.contentHeight.constant = 0;
    [UIView animateWithDuration:0.2 animations:^{
        self.contentHeight.constant = 44 * self.dataArray.count;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)removieView{
    self.show=NO;
    if (self.disblock) {
        self.disblock();
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.contentHeight.constant = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (IBAction)closeButton:(UIButton *)sender{
    [self removieView];
}

@end
